package org.dromara.web.appInterface;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.app.appParams.*;
import org.dromara.app.appReturn.AppProtocolRulesReturn;
import org.dromara.app.appReturn.AppUserInfo;
import org.dromara.app.appReturn.AppYueJuListReturn;
import org.dromara.app.domain.SkProtocolRules;
import org.dromara.app.domain.vo.SkProtocolRulesVo;
import org.dromara.app.domain.vo.SkVersionVo;
import org.dromara.app.service.ISkProtocolRulesService;
import org.dromara.app.service.ISkVersionService;
import org.dromara.app.service.ISkYueJuService;
import org.dromara.common.core.domain.R;
import org.dromara.system.mapper.SysConfigMapper;
import org.dromara.system.service.ISysConfigService;
import org.dromara.web.CssUtil.CssTiXianServer;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * @auther Cai Shuai Shuai
 * @data 2024/12/23
 * @apiNode
 */
@Tag(name = "登录相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/html")
@SaIgnore
public class AppHtmlInterface {

    private final ISkProtocolRulesService iSkProtocolRulesService;

    private final ISkVersionService iSkVersionService;

    private final ISysConfigService iSysConfigService;

    private final ISkYueJuService iSkYueJuService;

    private final CssTiXianServer cssTimeService;

    /**
     * 发送短信验证码
     */
    @GetMapping("/privacytext")
    @Operation(summary = "获取隐私协议", method = "post")
    public void privacytext(HttpServletResponse response) throws IOException {
//        response.setContentType("text/html,charset=utf-8");
        AppGuiZeParams app =  new AppGuiZeParams();
        app.setType("6");
        AppProtocolRulesReturn skProtocolRules = iSkProtocolRulesService.protocolRules(app);
//        PrintWriter out = response.getWriter();
////        PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));

        //在获取流对象之前告诉浏览器使用什么字符集
        response.setCharacterEncoding("utf-8");

        //告诉浏览器，服务器发送的消息体的数据的编码
        response.setHeader("content-type","text/html;charset=utf-8");

        //1.获取字符输出流
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>隐私协议</title>");
        out.println("</head>");
        out.println("<body>");
        out.println(skProtocolRules.getContent());
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }

    /**
     * 获取版本信息
     */
    @PostMapping("/getVersion")
    @Operation(summary = "获取版本信息", method = "post")
    public R<SkVersionVo> getVersion(){
        SkVersionVo app = iSkVersionService.getInfo();
        String number = iSysConfigService.selectConfigByKey("get_huiyuan_pt_user_number");
        app.setNumber(number);
        return R.ok(app);
    }


    /**
     * 约局详情
     */
    @PostMapping("/getYueJuInfo")
    @Operation(summary = "约局详情(H5)", method = "post")
    public R<AppYueJuListReturn> getYueJuInfo(@RequestBody AppDetailByIdParams params) {
        AppYueJuListReturn info = iSkYueJuService.getYueJuInfoNotLogin(params);
        return R.ok(info);
    }


    /**
     * 富友回调通知
     */
    @PostMapping("/getFuYouCallBack")
    @Operation(summary = "富友回调通知", method = "post")
    public R<Void> getFuYouCallBack(@RequestBody String app) throws Exception {
        AppFuYouCallBack appFuYouCallBack = new AppFuYouCallBack();
        appFuYouCallBack.setMessage(app);
        cssTimeService.getFuYouCallBack(appFuYouCallBack);
        return R.ok("请求成功");
    }



}
